package com.sctlqq.wuye.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sctlqq.wuye.entity.Notice;
import com.sctlqq.wuye.entity.Result;
import com.sctlqq.wuye.entity.User;
import com.sctlqq.wuye.service.UserService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户Controller（前端控制器）
 */
@RestController
@RequestMapping("/sctlqq/api/wuye/user")
public class UserController {

    @Autowired//自动实例化
    private UserService userService;

    /**
     * 查询所有 维修人员
     * @return
     */
    @GetMapping("/getworkerlist")
        public Result<Object> getWorkerList(){
            return new Result<>(HttpStatus.OK.value(),"success",userService.list(new QueryWrapper<User>().eq("usertype","维修人员").orderByDesc("id")));
    }

    /**
     * 查询所有 业主
     * @return
     */
    @GetMapping("/getownerlist")
    public Result<Object> getOwnerList(){
        return new Result<>(HttpStatus.OK.value(),"success",userService.list(new QueryWrapper<User>().eq("usertype","业主").orderByDesc("id")));
    }

    /**
     * 根据id 查询对应的 业主信息
     * @param id  业主id
     * @return
     */
    @PostMapping("/getownerbyid")
    public Result<Object> getOwnerById(@RequestBody Long id){
        return new Result<>(HttpStatus.OK.value(),"success",userService.getById(id));
    }

    /**
     * 登录
     * @return
     */
    @PostMapping("/login")
    public Result<Object> login(@RequestBody User user){
        List<Map> mapList=new ArrayList<>();
        User realUser= userService.getOne(new QueryWrapper<User>().eq("usertype",user.getUsertype())
                                                            .eq("username",user.getUsername()));
        Map<String, Boolean> map=new HashMap<>();
        Map<String, User> mapuser=new HashMap<>();
        if(realUser== null){
            map.put("isok",false);
            mapuser.put("user",null);
        }else{
            map.put("isok",user.getPassword().equals(realUser.getPassword()));
            mapuser.put("user",realUser);
            mapList.add(map);
            mapList.add(mapuser);
        }
        return new Result<>(HttpStatus.OK.value(),"success",mapList);
    }



    /**
     * 通过给定对象 新增单个维修人员
     * @param user
     * @return
     */
    @PostMapping("/adduser")
    public Result<Object> addWorker(@RequestBody User user){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",userService.save(user));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }

    /**
     * 通过给定对象 修改单个人员信息
     * @param user
     * @return
     */
    @PutMapping("/updateuser")
    public Result<Object> updateNotice(@RequestBody User user){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",userService.updateById(user));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }

    /**
     * 通过给定对象 删除单个或多个人员信息
     * @param userList
     * @return
     */
    @DeleteMapping("/deleteuser")
    public Result<Object> deleteNotice(@RequestBody List<User> userList){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",userService.removeBatchByIds(userList));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }



}
